//
//  AppDelegate.m
//  XJL_WeChat
//
//  Created by XJL on 16/7/24.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "AppDelegate.h"
#import "XJLLoginViewController.h"
#import <SMS_SDK/SMSSDK.h>
#import "AppDelegate+Theme.h"
#import "AppDelegate+EaseMob.h"

#define mobAppKey @"1597ce1610639"
#define mobAppSecret @"c6b1b5b9f58a76645b752d58941cbcec"


@interface AppDelegate ()<EMChatManagerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //设置主题
    [self setupTheme];
    char str[100];
    XJLLog(@"%ld",sizeof(str));

    void *p = malloc( 100 );
    XJLLog(@"%ld",sizeof(p));
    //短信验证码初始化
    [SMSSDK registerApp:mobAppKey
             withSecret:mobAppSecret];
    
    //registerSDKWithAppKey: 注册的AppKey，详细见下面注释。
    //apnsCertName: 推送证书名（不需要加后缀），详细见下面注释。
    [[EaseMob sharedInstance] registerSDKWithAppKey:@"xujiali#xjlweichat" apnsCertName:nil otherConfig:@{kSDKConfigEnableConsoleLogger:@(NO)}];
    [[EaseMob sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions];
    
//    先判断是否设置了自动登录
    BOOL isAutoLogin = [[EaseMob sharedInstance].chatManager isAutoLoginEnabled];
    // 3.如果没有登录过,来到主界面
    if (!isAutoLogin) {
        XJLLoginViewController *loginVc = [[XJLLoginViewController alloc]init];
        
        self.window.rootViewController = loginVc;
                
    }else{
        ///进入主界面
        UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        self.window.rootViewController = sb.instantiateInitialViewController;
        
        [self showUdhandleBuddyRequestCount];

    }
    
    [self.window makeKeyAndVisible];
    
    // 4.添加聊天管理器的代理
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    return YES;
}

// APP进入后台
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[EaseMob sharedInstance] applicationDidEnterBackground:application];
}

// APP将要从后台返回
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    [[EaseMob sharedInstance] applicationWillEnterForeground:application];
}

// 申请处理时间
- (void)applicationWillTerminate:(UIApplication *)application
{
    [[EaseMob sharedInstance] applicationWillTerminate:application];
}


@end
